import * as faceapi from "face-api.js";

init()

async function init(){
    //加载模型权重文件
    await faceapi.nets.ssdMobilenetv1.loadFromUri('http://127.0.0.1:8888/weights/')
    await faceapi.nets.faceLandmark68Net.loadFromUri('http://127.0.0.1:8888/weights/')
    await faceapi.nets.faceRecognitionNet.loadFromUri('http://127.0.0.1:8888/weights/')

    const input = document.getElementById('image')

    // 根据图片创建一个画布，用户显示人脸方框
    let canvas = faceapi.createCanvasFromMedia(input)
    const container = document.getElementById('box')
    container.prepend(canvas)
    const displaySize = { width:input.width, height:input.height }
    faceapi.matchDimensions(canvas, displaySize)

    //开始获取图片中每一张人脸的特征数据
    const options = new faceapi.SsdMobilenetv1Options({ minConfidence: 0.38 })
    const results = await faceapi.detectAllFaces(input, options)

    //将人脸在画布上标识出来
    faceapi.matchDimensions(canvas, input)
    faceapi.draw.drawDetections(canvas, faceapi.resizeResults(results,input))
    
    closeLoading()
}